/// ITT estimates over time 

********************************************************************************
***  Load data
use "data/pseudodata/userdaylevel_pseudodata.dta", clear	

keep if day>=0 & day<=90
replace day = day+1

replace hascov = 100*hascov
replace hasres = 100*hasres

*save levels for graphing
preserve
	keep id daily day hasres
	collapse (mean) hasres, by(day daily)
	reshape wide hasres, i(day) j(daily)
	tempfile levels
	save `levels'
restore

gen dailyday = daily*day

*estimate regression for confidence intervals
reghdfe hasres i.dailyday, vce(cluster id) a(day) nocons
regsave, ci

gen day = _n
replace day = day-1

merge 1:1 day using `levels'

gen coef_hi = hasres0+ci_upper
gen coef_lo = hasres0+ci_lower

replace day = day-1

graph twoway	(line hasres0 day) ///
				(line hasres1 day) ///
				(rarea coef_hi coef_lo day, col(maroon%15) lcol(maroon%0)), ///
					xtitle("Days from insurance take-up") ytitle("") xlab(0(30)90) ///
					ylab(0(5)20) ///
					subtitle("Has insurance through experiment (%)", pos(11) span) ///
					legend(order(1 "Traditional" 2 "Pay-as-you-go" 3 "ITT 95% CI") pos(1) ring(0))
	graph export "output/figures/ITT over time shade.pdf", replace


*** has any insurance
use "data/pseudodata/userhourlevel_pseudodata.dta", clear	

keep if hourcr>=0 & hourcr<=91*24

gen alldaycr = floor(daycr)
gen dayhourscr = 1
collapse (mean) anyins (sum) dayhourscr, by(id daily alldaycr)

assert alldaycr==91 if dayhourscr==1
drop if dayhourscr==1
assert dayhourscr==24

replace alldaycr = alldaycr+1

replace anyins = anyins*100

preserve
	keep id daily alldaycr anyins
	collapse (mean) anyins, by(alldaycr daily)
	reshape wide anyins, i(alldaycr) j(daily)
	tempfile levels
	save `levels'
restore

gen dailyday = daily*alldaycr

reghdfe anyins i.dailyday, vce(cluster id) a(alldaycr) nocons
regsave, ci

gen alldaycr = _n
replace alldaycr = alldaycr-1

merge 1:1 alldaycr using `levels'

gen coef_hi = anyins0+ci_upper
gen coef_lo = anyins0+ci_lower

replace alldaycr = alldaycr-1

graph twoway	(line anyins0 alldaycr) ///
				(line anyins1 alldaycr) ///
				(rarea coef_hi coef_lo alldaycr, col(maroon%15) lcol(maroon%0)), ///
					xtitle("Days from insurance application") ytitle("") xlab(0(30)90) ///
					ylab(0(10)40) ///
					subtitle("Has any insurance (%)", pos(11) span) ///
					legend(order(1 "Traditional" 2  "Pay-as-you-go" 3 "ITT 95% CI") pos(1) ring(0))
graph export "output/figures/ITT over time any shade.pdf", replace

